/**
 * 
 */
package com.xbang.attendance.helper.inventory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.stock.InventoryRecord;
import com.xbang.attendance.service.InventoryService;

import enumeration.InventoryOperation;

/**
 * @author dev.zhang
 * 
 */
public class InventoryHelper {
	
	@Autowired
	private InventoryService inventoryService;

	public static List<InventoryRecord> proccessInventoryRecords(List<InventoryRecord> inventoryRecords) {

		if (inventoryRecords == null || inventoryRecords.isEmpty()) {
			return inventoryRecords;
		}

		List<InventoryRecord> results = new ArrayList<InventoryRecord>();
		// 进行排序
		Collections.sort(inventoryRecords, new Comparator<InventoryRecord>() {

			public int compare(InventoryRecord sourceInventoryRecord, InventoryRecord indentInventoryRecord) {
				if (sourceInventoryRecord.getProduct().getProductCategory().getId() == indentInventoryRecord.getProduct().getProductCategory().getId()) {
					if (sourceInventoryRecord.getProduct().getProductCode().equals(indentInventoryRecord.getProduct().getProductCode())) {
						return 0;
					} else {
						return sourceInventoryRecord.getProduct().getId() > indentInventoryRecord.getProduct().getId() ? 1 : -1;
					}
				} else {
					return sourceInventoryRecord.getProduct().getProductCategory().getId() > indentInventoryRecord.getProduct().getProductCategory().getId() ? 1
							: -1;
				}
			}

		});

		// /拿到初始库存
		InventoryRecord compareInventoryRecord = null;
		BigDecimal totalAmount = new BigDecimal(0);

		for (InventoryRecord inventoryRecord : inventoryRecords) {

			if (compareInventoryRecord != null && compareInventoryRecord.getProduct().getId().equals(inventoryRecord.getProduct().getId())) {
				if (inventoryRecord.getLastInventoryOperation().compareTo(InventoryOperation.OUT) == 0) {
					totalAmount = totalAmount.subtract(inventoryRecord.getAmount());
				} else {
					totalAmount = totalAmount.add(inventoryRecord.getAmount());
				}
				results.add(inventoryRecord);
				compareInventoryRecord = inventoryRecord;
			} else {
				if (compareInventoryRecord != null) {
					// //汇总信息
					Inventory inventory = compareInventoryRecord.getInventory();
					
					BigDecimal agreementAmount = inventory.getAgreementAmount() == null ? new BigDecimal(0) : inventory.getAgreementAmount();
					InventoryRecord tmpTotleInventRecord = new InventoryRecord();
					tmpTotleInventRecord.setAmount(inventory.getAgreementAmount() == null ? new BigDecimal(0).subtract(totalAmount) : agreementAmount
							.subtract(totalAmount));
					tmpTotleInventRecord.setTotal(Boolean.TRUE);
					tmpTotleInventRecord.setProduct(compareInventoryRecord.getProduct());
					tmpTotleInventRecord.setInventory(inventory);
					results.add(tmpTotleInventRecord);
				}
				compareInventoryRecord = inventoryRecord;
				if (inventoryRecord.getLastInventoryOperation().compareTo(InventoryOperation.OUT) == 0) {
					totalAmount = new BigDecimal(0).subtract(inventoryRecord.getAmount());
				} else {
					totalAmount = inventoryRecord.getAmount();
				}
				results.add(inventoryRecord);
			}
		}
		if (compareInventoryRecord != null) {
			// //汇总信息
			Inventory inventory = compareInventoryRecord.getInventory();

			InventoryRecord tmpTotleInventRecord = new InventoryRecord();
			tmpTotleInventRecord.setAmount(inventory.getAgreementAmount() == null ? new BigDecimal(0).subtract(totalAmount) : inventory.getAgreementAmount()
					.subtract(totalAmount));
			tmpTotleInventRecord.setTotal(Boolean.TRUE);
			tmpTotleInventRecord.setProduct(compareInventoryRecord.getProduct());
			tmpTotleInventRecord.setInventory(inventory);
			results.add(tmpTotleInventRecord);
		}
		return results;
	}
}
